package com.cn.onetrip.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cn.onetrip.activity.R;
import com.cn.onetrip.untility.SysApplication;

/**
 * 
 * @FileName: SlideMenuLayout.java
 * 
 * @Package com.slide.menu
 * 
 * @Author Hanyonglu
 * 
 * @Version V1.0
 */
public class SlideMenuLayout {
	private ArrayList<TextView> menuList = null;

	private Activity activity;
	private TextView textView = null;
	private int mCount = 0;
	private Handler mHandler;
	private Map<String, Integer> ClickNumMap = new HashMap<String, Integer>();
	private int defaultNum = 10;

	public SlideMenuLayout(Activity activity, Handler handler) {
		this.activity = activity;
		this.mHandler = handler;
		menuList = new ArrayList<TextView>();
	}

	/**
	 * 
	 * @param menuTextViews
	 * @param layoutWidth
	 */
	public View getSlideMenuLinerLayout(String[] menuTextViews, int layoutWidth) {
		LinearLayout menuLinerLayout = new LinearLayout(activity);
		menuLinerLayout.setOrientation(LinearLayout.HORIZONTAL);

		LinearLayout.LayoutParams menuLinerLayoutParames = new LinearLayout.LayoutParams(
				menuTextViews.length == 1 ? layoutWidth : layoutWidth / 2,
				LinearLayout.LayoutParams.WRAP_CONTENT, 1);
		menuLinerLayoutParames.gravity = Gravity.CENTER_HORIZONTAL;

		for (int i = 0; i < menuTextViews.length; i++) {
			TextView tvMenu = new TextView(activity);
			tvMenu.setTag(menuTextViews[i]);
			tvMenu.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT));
			tvMenu.setPadding(
					30,
					activity.getResources().getDimensionPixelOffset(
							R.dimen.route_bar_top),
					30,
					activity.getResources().getDimensionPixelOffset(
							R.dimen.route_bar_bottom));
			tvMenu.setText(menuTextViews[i]);
			tvMenu.setTextColor(Color.WHITE);
			tvMenu.setGravity(Gravity.CENTER_HORIZONTAL);
			tvMenu.setOnClickListener(SlideMenuOnClickListener);
			if (mCount == 0) {
				tvMenu.setBackgroundResource(R.drawable.route_selector);
			}
			ClickNumMap.put(menuTextViews[i], mCount + 10);
			menuLinerLayout.addView(tvMenu, menuLinerLayoutParames);
			menuList.add(tvMenu);
			mCount++;
		}

		return menuLinerLayout;
	}

	OnClickListener SlideMenuOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			if (SysApplication.visitsListObj.size() == 1) {
				return;
			}
			String menuTag = null;
			try {
				menuTag = v.getTag().toString();
			} catch (NullPointerException e) {
				return;
			}
			if (v.isClickable()) {
				int a = ClickNumMap.get(menuTag);
				if (a == defaultNum) {
					return;
				}
				defaultNum = a;
				textView = (TextView) v;
				textView.setBackgroundResource(R.drawable.route_selector);

				for (int i = 0; i < menuList.size(); i++) {
					if (!menuTag.equals(menuList.get(i).getText())) {
						menuList.get(i).setBackgroundDrawable(null);
					}
				}
				slideMenuOnChange(menuTag);

				Message msg = Message.obtain();
				msg.what = defaultNum;
				mHandler.sendMessage(msg);
			}
		}
	};

	private void slideMenuOnChange(String menuTag) {
		// LayoutInflater inflater = activity.getLayoutInflater();
		// ViewGroup llc = (ViewGroup) activity
		// .findViewById(R.id.linearLayoutContent);
		// llc.removeAllViews();

		// if (menuTag.equals(SlideMenuUtil.ITEM_MOBILE)) {
		// llc.addView(inflater.inflate(R.layout.item_mobile, null));
		// } else if (menuTag.equals(SlideMenuUtil.ITEM_WEB)) {
		// llc.addView(inflater.inflate(R.layout.item_web, null));
		// } else if (menuTag.equals(SlideMenuUtil.ITEM_CLOUD)) {
		// llc.addView(inflater.inflate(R.layout.item_cloud, null));
		// } else if (menuTag.equals(SlideMenuUtil.ITEM_DATABASE)) {
		// llc.addView(inflater.inflate(R.layout.item_database, null));
		// } else if (menuTag.equals(SlideMenuUtil.ITEM_EMBED)) {
		// llc.addView(inflater.inflate(R.layout.item_embed, null));
		// } else if (menuTag.equals(SlideMenuUtil.ITEM_SERVER)) {
		// llc.addView(inflater.inflate(R.layout.item_server, null));
		// } else if (menuTag.equals(SlideMenuUtil.ITEM_DOTNET)) {
		// llc.addView(inflater.inflate(R.layout.item_dotnet, null));
		// } else if (menuTag.equals(SlideMenuUtil.ITEM_JAVA)) {
		// llc.addView(inflater.inflate(R.layout.item_java, null));
		// } else if (menuTag.equals(SlideMenuUtil.ITEM_SAFE)) {
		// llc.addView(inflater.inflate(R.layout.item_safe, null));
		// } else if (menuTag.equals(SlideMenuUtil.ITEM_DOMAIN)) {
		// llc.addView(inflater.inflate(R.layout.item_domain, null));
		// } else if (menuTag.equals(SlideMenuUtil.ITEM_RESEASRCH)) {
		// llc.addView(inflater.inflate(R.layout.item_research, null));
		// } else if (menuTag.equals(SlideMenuUtil.ITEM_MANAGE)) {
		// llc.addView(inflater.inflate(R.layout.item_manage, null));
		// }
	}
}